package com.zer0t3ch.mcServerControl;

import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/*
 * @author Zer0t3ch
 */
public class DatabaseManager {
    public static Connection connection = null;
    
    public static boolean sqlInit() throws ClassNotFoundException {
        System.out.println("SQL Initializing");
        Class.forName("org.sqlite.JDBC");
        makePrefFolder();
        try {
            connection = DriverManager.getConnection("jdbc:sqlite:" + Main.workingDirectory + "servers.db");
            return true;
        }
        catch(SQLException e) {
            System.out.println(e.toString());
            return false;
        }
    }
    
    public static boolean sqlCleanup() {
        System.out.println("Cleaning up the SQL");
        try {
            if(connection != null) {
                connection.close();
                return true;
            }
            else
                return false;
        }
        catch (SQLException e) {
            System.out.println(e.toString());
            return false;
        }
    }
    
    public static boolean makePrefFolder() {
        if (new File(Main.workingDirectory).mkdir()) {
            return true;
        }
        else {
            return false;
        }
    }
}
